import { fileURLToPath, URL } from "node:url";

import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
import vueDevTools from "vite-plugin-vue-devtools";
import AutoImport from "unplugin-auto-import/vite";
import Components from "unplugin-vue-components/vite";
import { ElementPlusResolver } from "unplugin-vue-components/resolvers";

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueDevTools(),
    AutoImport({
      resolvers: [ElementPlusResolver()],
    }),
    Components({
      resolvers: [ElementPlusResolver()],
    }),
  ],
  resolve: {
    alias: {
      "@": fileURLToPath(new URL("./src", import.meta.url)),
    },
  },
  server: {
    host: true,
    port: 5006,
    // 修改 vite.config.js 代理配置
    proxy: {
      "/goods/api": {
        target: "http://127.0.0.1:3006",
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/goods\/api/, ""),
      },
      "/users/api": {
        target: "http://127.0.0.1:3006",
        changeOrigin: true,
        rewrite: (path) => path.replace(/^\/users\/api/, ""),
      },
      "/api": {
        target: "http://127.0.0.1:3006",
        changeOrigin: true, // 允许局域网访问（如手机/其他电脑访问开发环境）
        rewrite: (path) => path.replace(/^\/api/, ""),
      },
      "/upfile": {
        target: "http://127.0.0.1:3006",
        changeOrigin: true,
      },
    },
  },
});
